import java.awt.*;
import java.applet.Applet;

import javax.swing.JFrame;

public class Grid extends Applet{
	
	JFrame frame;
	Label[][] labels;
    
	/**
	 * Constructor - create the colored grid
	 * @param rows - number of rows in the grid
	 * @param cols - number of columns in the grid
	 */
	public Grid(int rows, int cols) {
		this(rows, cols, null);
	}
	
	/**
	 * Constructor - create the colored grid
	 * @param rows - number of rows in the grid
	 * @param cols - number of columns in the grid
	 * @param c - a component to be relevant to
	 */
    public Grid(int rows, int cols, Component c){
    	frame = new JFrame("Coloring Visualisation");
    	frame.setLayout(new GridLayout(rows,cols));
        frame.setSize(cols*30, rows*30);
        
        frame.setLocationRelativeTo(c);
        
        
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	labels = new Label[rows][cols];
    	for (int i=0;i<rows;i++)
    		for (int j=0;j<cols;j++)
    		{
    			labels[i][j]=new Label();
    			frame.add(labels[i][j]);
    		}
    }
    
    /**
     * set a color in a specific location
     * @param row - location to set color
     * @param col - location to set color
     * @param color - the number of the color (max - 10 colors)
     */
    public void setColor(int row, int col, int color){
    	if (color < 10){
    		Color c;
    		switch (color) {
			case 0:
				c=Color.blue;
				break;
			case 1:
				c=Color.red;
				break;
			case 2:
				c=Color.green;
				break;
			case 3:
				c=Color.yellow;
				break;
			case 4:
				c=Color.black;
				break;
			case 5:
				c=Color.orange;
				break;
			case 6:
				c=Color.cyan;
				break;
			case 7:
				c=Color.magenta;
				break;
			case 8:
				c=Color.pink;
				break;
			default:
				c=Color.white;
				break;
			}
    		labels[row][col].setBackground(c);
    	}
    		
    }
    
    /**
     * show the grid to screen
     */
    public void show(){
    	frame.setVisible(true);
    }
}
